<?php
	class TemplateData {
		var $data;
		var $verbose;
		var $temporal;
		
		function TemplateData($data=array()){
			$this->data = $data;
			$this->verbose = FALSE;
			$this->temporal = array();
		}
		
		function setTemporal($dataName,$dataValue){
			if(array_search($dataName,$this->temporal)===FALSE) $this->temporal[] = $dataName;
			$this->data[$dataName] = $dataValue;
		}
		
		function clear($dataName){
			if($dataName==NULL){
				/*
					TODO Eliminar todos los datos temporales
				*/
			}else if(array_search($dataName,$this->temporal)!==FALSE){
				unset($this->data[$dataName]);
			}
		}
		
		function get($obj_path){
			$postProcess = explode(":",$obj_path);
			$obj_path = array_shift($postProcess);
			//$postProcess = array_reverse($postProcess);
			/*
			echo "OBJECT-PATH : $obj_path\n";
			echo "COMIENZA =====================\n";
			var_dump($postProcess);
			var_dump($obj_path);
			echo "TERMINA ======================\n";
			*/
			/*
			$verbose  =false;
			if($obj_path=="sections.count"){
				$verbose = true;
			}
			if($verbose) echo $obj_path;
			*/
			$field = explode(".",$obj_path);
			$root = strToLower(array_shift($field));
			if(array_key_exists($root,$this->data)!==FALSE){
				$object = $this->data[$root];
				while(count($field)>0){
					$subobject = array_shift($field);
					/*
					if($verbose) {
						echo "<h2>".$subobject."</h2>";
						echo "<h2>".$object."</h2>";
						echo "<b>VARDUMP</b><br>";
						echo "array ".is_array($object)."<br>";
						echo "object ".is_object($object)."<br>";
						echo "<pre>";
						var_dump($object);
						echo "</pre>";
					}
					*/
					if(
						(is_object($object) && array_key_exists($subobject,get_object_vars($object)))
						||
						(is_array($object) && array_key_exists($subobject,$object))
						||
						isset($object->$subobject)
					){
						//($object->$subobject)){
						$object = $object->$subobject;
					}else{
						$object = 0;
						//$object = null;
						//$object = $this->logic($subobject,$object);
						/*if($object==NULL){
							$object = ($this->verbose) ? $obj_path : "";
						}*/
						break;
					}
				}
			}else if($root=="session"){
				$subobject = array_shift($field);
				if(array_key_exists($subobject,$_SESSION)){
					$object = $_SESSION[$subobject];
					while(count($field)>0){
						$subobject = array_shift($field);
						if(
							(is_object($object) && array_key_exists($subobject,get_object_vars($object)))
							||
							(is_array($object) && array_key_exists($subobject,$object))
							||
							isset($object->$subobject)
						){
							$object = $object->$subobject;
						}else{
							$object = 0;
							//$object = null;
							//$this->logic($subobject,$object);
							break;
						}
					}
				}else{
					$object = 0;
					//$object = null;
					//($this->verbose) ? $obj_path : "";
				}
			}else if(intval($root)>0){
				$object = intval($root);
				/*if(count($field)>0){
					$subobject = array_shift($field);
					$object = $this->logic($subobject,$object);
				}*/
			}else{
				$object = ($this->verbose) ? $obj_path : "";
			}
			foreach($postProcess as $method){
					$object = $this->logic($method,$object);
			}
			return $object;
		}
		
		function logic($type,$object){
			$return = NULL;
			/*
				TODO: Add multiple modifiers (separados por :??)
			*/
			switch(strToLower($type)){
				// DATE CASES
				case "month":
					$return = date("m",strtotime($object));
					break;
				case "fullmonth":
					$return = date("F",strtotime($object));
					break;
				case "year":
					$return = date("Y",strtotime($object));
					break;
				case "date":
					$return = date("d",strtotime($object));
					break;
				/* mailto */
				case "mailto":
					$return = mail_tag($object);
					break;
				case "empty":
					$return = intval($object==NULL);
					break;
				case "human":
					$return = TimeDate::human($object);
					break;
				case "count":
					$return = intval(count($object));
					break;
				case "toup":
					$return = strToUpper($object);
					break;
				case "tolow":
					$return = strToLower($object);
					break;
				case "resume":
					$return = resume($object);
					break;
				case "textile":
					$return = textile($object);
					break;
				case "humansize":
					$sizeNames = array("bytes","Kb","Mb","Gb","Tb");
					$s = 0;
					while($object>1024){
						$object = $object/1024;
						$s++;
					}
					$return = (round($object*10))/10 . " ".$sizeNames[$s];
					break;
				case "random":
					if(is_array($object)){
						$return = $object[rand(0,count($object))];
					}else{
						$return = rand(0,$object);
					}
					break;
				default:
					$return = ($this->verbose) ? $obj_path : "";
					break;
			}
			return $return;
		}
	}
?>